<template>
    <div>
        <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border default-expand-all
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column prop="uid" label="UID" width="200">
            </el-table-column>
            <el-table-column prop="username" label="标题" width="120">
            </el-table-column>

            <el-table-column label="状态" width="180">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
                    <el-tag v-else type="info">禁用</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(scope.row)" circle>
                    </el-button>
                    <el-button @click="del(scope.row.id)" size="mini" type="danger" icon="el-icon-delete" circle>
                    </el-button>
                </template>
            </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize"
            layout="total, prev, pager, next, jumper" :total="total">
        </el-pagination>
    </div>
</template>

<script>
import { getuserList, getuserCount } from '../../apis'
export default {
    data() {
        return {
            tableData: [],
            //数据总量
            total: 0,
            //当前显示第几页
            currentPage: 1,
            //每页显示几条数据
            pageSize: 1
        };
    },
    created() {
        this.getTableNum();
    },
    methods: {
        async getTableNum() {
            let res = await getuserCount();
            console.log(res);
            this.total = res.list[0].total;
            this.getTableData()//获取分页的数据
            //size显示多少条数据  page显示第几页
        },
        async getTableData() {
            let res = await getuserList({ size: this.pageSize, page: this.currentPage })
            console.log(res);
            this.tableData = res.list;
        },
        //跳转页面
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getTableData()//获取分页的数据
            console.log(`当前页: ${val}`);
        },
        edit(info) {
            // 点击编辑按钮 要通过index组件打开info组件 并传值
            this.$emit('edit', info)
        },
        del(id) {
            //打开弹窗，询问是否要真的删除
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                // 点击确定按钮就执行then方法中的内容
                // 此时 真的删除了
                let res = await delMenuById(id)
                console.log(9999, res);
                if (res.code == 200) {
                    // 删除成功后再弹窗
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // 删除后更新
                    this.tableData = res.list;
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
            console.log(id);
        },
    },
}
</script>

<style>

</style>